package edu.zut.cs.network.exercise.bank.service.impl;


import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import edu.zut.cs.network.exercise.bank.dao.BankDao;
import edu.zut.cs.network.exercise.bank.dao.impl.BankDaoImpl;
import edu.zut.cs.network.exercise.bank.entity.Bank;
import edu.zut.cs.network.exercise.bank.service.BankManager;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;



public class BankManagerImpl implements BankManager{

	protected Logger logger = LogManager.getLogger(this.getClass().getName());
	List<Bank> bankList;
	BankDao bankDao = new BankDaoImpl();
	
	public void loadAdd(String data_file) {
		this.bankList = new ArrayList<Bank>();
		InputStream input = BankManagerImpl.class.getResourceAsStream(data_file);
		XSSFWorkbook wb = null;
		try {
			wb = new XSSFWorkbook(input);
		} catch (IOException e) {
			e.printStackTrace();
		}
		XSSFSheet sheet = wb.getSheetAt(1);
		for (int i = 1; i <= sheet.getLastRowNum(); i++) {
			Row row = sheet.getRow(i);
			Bank s = new Bank();
			/**
			 * Set the system unique Id for object
			 */
			s.setId(Long.valueOf(1000 + i));
			for (int j = 0; j < row.getLastCellNum(); j++) {
				Cell cell = row.getCell(j);
				if (cell != null) {
					cell.setCellType(CellType.STRING);
					String value = row.getCell(j).getStringCellValue().trim();
					if (j == 0)
						s.setName(value);
					if (j == 1)
						s.setAge(value);
					if (j == 2)
						s.setMoney(value);
				}
			}
			logger.info(" bank_" + i + " is :" + s);
			this.bankList.add(s);
			this.bankDao.add(s); //添加到数据库 
		}
		logger.info("size of bank list is :" + this.bankList.size());

	}

	public Bank findByNameBank(String name) {

		return bankDao.findByName(name);
	}

	public List<Bank> queryAllComp() {

		return bankDao.queryall();
	}

	public void upDateBank(Bank bank) {

		bankDao.upDate(bank);
	}

	public void deleteBank(Bank bank) {

		bankDao.delete(bank);
	}

	public String json(List<Bank> bankList) {
		//computerList = producerDao.queryall();
		Iterator<Bank> iterator = bankList.iterator();
		JSONObject j = new JSONObject();
		JSONArray array = new JSONArray();
		while (iterator.hasNext()) {
			Bank bank = new Bank();
			bank = iterator.next();
			JSONObject jcomputer = new JSONObject();
			jcomputer.accumulate("name", bank.getName());
			jcomputer.accumulate("age", bank.getAge());
			jcomputer.accumulate("money", bank.getMoney());
			array.add(jcomputer);
		}
		j.accumulate("Bank", array);
		//System.out.println(j.toString());
		return j.toString();
	}

}
